<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="商品列表">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
    </style>

    <script>
        jQuery(function () {
            fixTableOperatorButton();

            $(".close").click(function () {
                closeAll();
            });

            ajaxSelects("${baseUrl}goods/type/getAll.action",[
                {selectName:"typeId",head:"商品分类" },
                {selectName:"moveTypeId" }]);
        });

        layui.use(['form', 'table'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                util = layui.util;

            renderTable();


            table.on('tool(table)', function (obj) {
                let data = obj.data;
                switch(obj.event){
                    case 'detail':
                        openTab("商品<"+data.name+">详情","${baseUrl}goods/goodsDetail.action?id="+data.id);
                        break;
                    case 'delete':
                        del(data);
                        break;
                    case 'edit':
                        openTab("商品<"+data.name+">编辑","${baseUrl}goods/goodsEdit.action?id="+data.id);
                        break;
                };
            });

            table.on('toolbar(table)', function (obj) {
                switch(obj.event){
                    case 'add':
                        openTab("新增商品","${baseUrl}goods/goodsNew.action");
                        break;
                    case 'quickPrice':
                        openTab("商品改价","${baseUrl}goods/goodsQuickPrice.action");
                        break;
                    case 'export':
                        openMiddleDialogByTagId("商品导出", $("#exportDialog"));
                        break;
                    case 'import':
                        openTab("商品导入","${baseUrl}goods/goodsImport.action");
                        break;
                    case 'printCode':
                        let data = table.checkStatus('table').data;
                        if (checkSelectNum(data)){
                            document.getElementById("printFrame").contentWindow.printPage(data);
                        }
                        break;
                    case 'sn':
                        openSn(table.checkStatus('table').data);
                        break;
                    case 'enable':
                        batchEnable(table.checkStatus('table').data);
                        break;
                    case 'disable':
                        batchDisable(table.checkStatus('table').data);
                        break;
                    case 'delete':
                        batchDelete(table.checkStatus('table').data);
                        break;
                    case 'moveType':
                        if (checkSelectNum(table.checkStatus('table').data)){
                            openMiddleDialogByTagId("移动至分类", $("#moveTypeDialog"));
                        }
                        break;
                };
            });

            form.on('checkbox(noClose)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
            });

            form.on('submit(search)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
                return false;
            });

        });

        function renderTable() {
            layui.table.render({
                id:'table',
                elem: '#table',
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                url: "${baseUrl}goods/goods/getList.action",
                where : {
                    status :1,
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {type:'checkbox', fixed:'left'},
                    {width:70,title:"序号",totalRowText: '合计',templet:function (d) {
                            return genTableIndex(d.status == 2, "停用", d);
                    }},
                    {field: 'name', minWidth: 120, title: '名称',style:"font-size:12px;"},
                    {field: 'no', minWidth: 120, title: '编号',style:"font-size:12px;"},
                    {field: 'type', width: 130, title: '分类',style:"font-size:12px;",templet: function(d){
                            return d.type.name;
                        }},
                    {width: 120, title: '属性',style:"font-size:12px;",templet: function(d){
                            return genAttr(d.attrNameVoList);
                    }},
                    {width: 80, title: '单位',style:"font-size:12px;",templet: function(d){
                        return d.goodsUnitVoList[0].unit.name;
                    }},
                    {width: 80, title: '零售价',style:"font-size:12px;",templet: function(d){
                            return getPrice(d.goodsUnitVoList[0].retailPrice);
                    }},
                    <c:if test="${industryType.type eq 2}">
                    {width: 80, title: '序列号',style:"font-size:12px;",templet: function(d){
                            return isTrue(d.sn) ? "启用" : "停用";
                    }},
                    </c:if>
                    {title: '操作', minWidth: 80,fixed:'right', templet: '#operBar'},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    setTableLineCss("table",res.data, "status", 2, "font-gray-c2c2c2");
                }
            });

            layui.table.on('tool(table)', function (obj) {
                let data = obj.data;
                if (obj.event === 'delete') {
                    del(data);
                }
            });
        }

        function del(data) {
            layer.confirm('删除商品《'+data.name+'》?', {icon: 3, title:'删除商品'}, function(index){
                tipLoad("商品删除中..");
                post("${baseUrl}goods/goods/delete.action",{id:data.id}, function (resp) {
                    closeLoading();
                    if (resp.code == 1){
                        closeAll();
                        tip("删除成功");
                        renderTable();
                    } else {
                        errTip(resp.msg);
                    }
                });
            });
        }

        function openSn(data) {
            if (checkSelectNum(data)){
                confirm("确定开启序列号吗？开启后每个商品都必须维护一个序列号。",function () {
                    tipLoad("处理中，请稍后");
                    post("${baseUrl}goods/goods/batchOpenSn.action",genIds(data),function (resp) {
                        closeAll();
                        if (resp.code == 1) {
                            layui.table.reload('table',{page: {curr: 1}});
                        }  else if (resp.code == 3) {
                            layui.table.reload('table',{page: {curr: 1}});
                            processFailInfo("开启序列号", resp.data);
                        } else {
                            errTip(resp.msg);
                        }
                    });
                });
            }
        }

        function batchEnable(data) {
            if (checkSelectNum(data)){
                tipLoad("处理中，请稍后");
                post("${baseUrl}goods/goods/batchEnable.action",genIds(data),function (resp) {
                    closeLoading();
                    if (resp.code == 1) {
                        layui.table.reload('table',{page: {curr: 1}});
                    }  else {
                        errTip(resp.msg);
                    }
                });
            }
        }

        function batchDisable(data) {
            if (checkSelectNum(data)){
                tipLoad("处理中，请稍后");
                post("${baseUrl}goods/goods/batchDisable.action",genIds(data),function (resp) {
                    closeLoading();
                    if (resp.code == 1) {
                        layui.table.reload('table',{page: {curr: 1}});
                    } else if (resp.code == 3) {
                        closeAll();
                        layui.table.reload('table',{page: {curr: 1}});
                        processFailInfo("以下商品停用失败,原因如下", resp.data);
                    } else {
                        errTip(resp.msg);
                    }
                });
            }
        }

        function batchDelete(data) {
            if (checkSelectNum(data)){
                confirm("确认删除选中的商品?",function () {
                    tipLoad("处理中，请稍后");
                    post("${baseUrl}goods/goods/batchDelete.action",genIds(data),function (resp) {
                        closeLoading();
                        if (resp.code == 1) {
                            closeAll();
                            layui.table.reload('table',{page: {curr: 1}});
                        }  else if (resp.code == 3) {
                            closeAll();
                            layui.table.reload('table',{page: {curr: 1}});
                            processFailInfo("批量删除商品成功,以下商品不允许删除,原因如下", resp.data);
                        } else {
                            errTip(resp.msg);
                        }
                    });
                });
            }
        }


        function genSearch() {
            let json = getFormJson($("#searchForm"));
            json.status = $('[name="noClose"]').next().hasClass("layui-form-checked") ? 1:'';
            return json;
        }
    </script>

</html:header>
<body>
<iframe id="printFrame" style="width:0px;height:0px" src="${baseUrl}print/printCode.action" frameborder="0"></iframe>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="table-tool-div">
            <form class="layui-form layui-form-pane" action="" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="checkbox" name="noClose" title="不显示停用商品" lay-skin="primary" value="true" checked lay-filter="noClose">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">其他信息</label>
                        <div class="layui-input-inline" style="min-width: 200px;">
                            <input type="text" id="search" name="search" style="font-size: 12px;" placeholder="编号、名称、规格、属性、条形码" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <a class="layui-btn " lay-submit="" lay-filter="search" id="btn-search"><i class="layui-icon layui-icon-search"></i></a>
                        <a class="layui-btn " id="selectSearch">筛选</a>
                    </div>

                    <div class="table-tool-div-screen" status="close" style="display: none">
                        <div class="layui-inline">
                            <label class="layui-form-label">商品分类</label>
                            <div class="layui-input-inline">
                                <select name="typeId">
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">商品编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="no" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">商品属性</label>
                            <div class="layui-input-inline">
                                <input type="text" name="attr" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">商品规格</label>
                            <div class="layui-input-inline">
                                <input type="text" name="norms" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-inline">
                                <input type="text" name="remark" class="layui-input">
                            </div>
                        </div>

                        <c:if test="${industryType.type eq 2}">
                        <div class="layui-inline">
                            <label class="layui-form-label">序列号状态</label>
                            <div class="layui-input-inline" style="width: 90px;">
                                <select name="sn">
                                    <option value="">不限</option>
                                    <option value="true">启用</option>
                                    <option value="false">停用</option>
                                </select>
                            </div>
                        </div>
                        </c:if>

                        <div class="layui-inline">
                            <a class="layui-btn" lay-submit="" lay-filter="search"><i class="layui-icon layui-icon-search"></i></a>
                            <a class="layui-btn layui-btn-primary" id="clearSearch">清空条件</a>
                        </div>

                        <script>
                            $("#selectSearch").click(function () {
                                if ($(".table-tool-div-screen").attr("status")=="close") {
                                    $(".table-tool-div-screen").css("display","block");
                                    $(".table-tool-div-screen").attr("status","open");
                                    $("#search").parent().parent().css("display","none");
                                    $("#search").val("");
                                    $("#btn-search").css("display","none");
                                    $(this).html("收起");
                                } else {
                                    $(".table-tool-div-screen").css("display","none");
                                    $(".table-tool-div-screen").attr("status","close");
                                    $("#search").parent().parent().css("display","inline-block");
                                    $("#btn-search").css("display","inline-block");
                                    $(this).html("筛选");
                                }
                            });

                            $("#clearSearch").click(function () {
                                $("#searchForm")[0].reset();
                            });
                        </script>
                    </div>
                </div>
            </form>
        </div>

        <div class="horizon-line"></div>

        <table class="layui-hide" id="table" lay-filter="table"></table>

        <script type="text/html" id="tableToolBar">
            <div class="layui-inline mb4">
                <a class="layui-btn" lay-event="add">新增</a>
            </div>

            <div class="layui-inline mb4">
                <a class="layui-btn" lay-event="quickPrice">改价</a>
            </div>

            <div class="layui-inline mb4">
                <html:drop-menu menu="导入" lay_active="batchOperation">
                    <a href="javascript:;" lay-event="import">导入</a>
                    <a href="javascript:;" lay-event="export">导出</a>
                </html:drop-menu>
            </div>

            <div class="layui-inline mb4">
                <html:drop-menu menu="批量操作" lay_active="">
                    <a href="javascript:;" lay-event="printCode">打印条码表</a>
                    <c:if test="${industryType.type eq 2}">
                        <a href="javascript:;" lay-event="sn">开启序列号</a>
                    </c:if>
                    <a href="javascript:;" lay-event="enable">启用</a>
                    <a href="javascript:;" lay-event="disable">停用</a>
                    <a href="javascript:;" lay-event="moveType">移动至分类</a>
                    <a href="javascript:;" lay-event="delete">删除</a>
                </html:drop-menu>
            </div>

        </script>

        <script type="text/html" id="operBar">
            <a class="layui-btn layui-btn-xs layui-btn-normal table-operator" lay-event="detail">详情</a>
            <a class="layui-btn layui-btn-xs table-operator" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger table-operator" lay-event="delete">删除</a>
        </script>
    </div>
</div>

</body>



<div id="moveTypeDialog" class="myDialog">
    <div>
        <form class="layui-form" id="moveTypeForm">
            <select name="moveTypeId">
            </select>
        </form>
        <hr>
        <button class="layui-btn" id="sureMoveType">确认</button>
        <button class="layui-btn layui-btn-primary close">取消</button>
    </div>
    <script>
        $("#sureMoveType").click(function () {
            let data = layui.table.checkStatus('table').data;
            if (!checkSelectNum(data)){
                return;
            }

            tipLoad("处理中，请稍后");
            let formData = getFormJson($("#moveTypeForm"));
            post("${baseUrl}goods/goods/batchMoveType.action?typeId="+formData.moveTypeId,genIds(data),function (resp) {
                closeLoading();
                if (resp.code == 1) {
                    closeAll();
                    layui.table.reload('table',{page: {curr: 1}});
                }  else {
                    errTip(resp.msg);
                }
            });
        });
    </script>
</div>

<div id="exportDialog" class="myDialog">
    <table class="layui-table">
        <colgroup>
            <col>
            <col>
            <col width="60">
        </colgroup>
        <thead>
        <tr>
            <th>门店</th>
            <th>仓库</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="exportDialogTBody">
        </tbody>
    </table>

    <hr>
    <button class="layui-btn exportAll">导出全部</button>
    <button class="layui-btn layui-btn-primary close">关闭</button>

    <script>
        layui.use(['form', 'table'], function () {
            let $ = layui.jquery,
                table = layui.table;

            let ajax = get("${baseUrl}organization/house/listNormalVo.action", function (resp) {
                if (resp.code == 1) {
                    $("#exportDialogTBody").empty();
                    $.each(resp.data, function (i,houseVo) {
                        let child = "<tr>\n" +
                            "    <td>" +houseVo.store.name+"</td>\n" +
                            "    <td>" +houseVo.name+"</td>\n" +
                            "    <td>\n" +
                            "        <a class='exportOne' houseId='" +houseVo.id+"' houseName='" +houseVo.name+"' href='javascript:void(0);'>导出</a>\n" +
                            "    </td>\n" +
                            "</tr>";
                        $("#exportDialogTBody").append(child);
                    });
                    formRender();
                }
            });

            $.when(ajax).done(function () {
                $(".exportOne").click(function () {
                    tipLoad("处理中...");
                    let houseId = $(this).attr("houseId");
                    console.log("houseId:" + houseId);

                    post("${baseUrl}goods/goods/export.action" + genParam(genSearch()) + "&houseId="+houseId, {},function (resp) {
                        if (resp.code == 1) {
                            downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                            setTimeout(function () {
                                closeAll();
                            },500);
                        } else {
                            closeLoading();
                            tip(resp.msg);
                        }
                    });
                });

                $(".exportAll").click(function () {
                    tipLoad("处理中...");
                    post("${baseUrl}goods/goods/export.action" + genParam(genSearch()), {},function (resp) {
                        if (resp.code == 1) {
                            downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                            setTimeout(function () {
                                closeAll();
                            },1000);
                        } else {
                            closeLoading();
                            tip(resp.msg);
                        }
                    });
                });
            });
        });
    </script>
</div>
<html:failInfoDialog/>
<html:loading/>
</html>